# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import pymysql

class SpiderPipeline(object):
    def process_item(self, item, spider):
        print('开始调用管道')
        db = pymysql.connect("localhost","root","123456","demo" )
        cursor = db.cursor()
        sql = "insert into book80txt(type,bookName,bookUrl,bookAuthor) values('测试','%s','%s','%s')"%(item['bookName'],item['bookUrl'],item['bookAuthor'])
        print(sql)
        try:
            # 执行sql语句
            cursor.execute(sql)
            # 执行sql语句
            db.commit()
        except:
            # 发生错误时回滚
            db.rollback()
            # 关闭数据库连接
            db.close()
        return item
